package com.alibaba.citrus.ots.unified.inventory.facade.service;


import com.alibaba.citrus.ots.unified.inventory.repository.SalesOrderWriteRepository;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsunifiedinventory.api.allocateorderwrite.service.AllocateOrderWriteService;
import com.epoch.app.otsunifiedinventory.api.relatedwmsorderwrite.service.RelatedWmsOrderWriteService;
import com.epoch.app.otsunifiedinventory.dto.request.AllocateOrderReceivedRequest;
import com.epoch.app.otsunifiedinventory.dto.request.AllocateOrderShippedRequest;
import com.epoch.app.otsunifiedinventory.dto.request.SalesOrderShipRequest;
import com.epoch.app.otsunifiedinventory.dto.request.WmsRelatedOrderUpdateRequest;
import com.epoch.app.otsunifiedinventory.dto.response.AllocateOrderReceivedResponse;
import com.epoch.app.otsunifiedinventory.dto.response.AllocateOrderShippedResponse;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author zhangguoqing
 * @date 2021.09.22
 */
public class RelatedWmsOrderWriteServiceImpl implements RelatedWmsOrderWriteService {
    @Autowired
    private AllocateOrderWriteService allocateOrderWriteService;

    @Autowired
    private SalesOrderWriteRepository salesOrderWriteRepository;

    @Override
    public Result<Object> updateRelatedOrderAfterCreateWmsOrder(WmsRelatedOrderUpdateRequest request) {
        //1.参数校验
        //2.更新调拨单/销售订单
        //3.更新履约单/发货单
        if (request.getOrderType().equals(1)) {//入库
            if (request.getBizType().equals(1)) {//调拨单
                AllocateOrderReceivedRequest receivedRequest = new AllocateOrderReceivedRequest();
                Result<AllocateOrderReceivedResponse> receiveResult = allocateOrderWriteService.received(receivedRequest);
                if (!receiveResult.isSuccess()) {
                    throw new RuntimeException("更新发货单失败");// TODO: 2021/9/22 定义统一的异常
                }

            } else {//销售单
                SalesOrderShipRequest salesOrderShipRequest = new SalesOrderShipRequest();
                Boolean saleResult = salesOrderWriteRepository.salesOrderShip(salesOrderShipRequest);
            }
        } else {//出库
            if (request.getBizType().equals(1)) {//调拨单
                AllocateOrderShippedRequest shipRequest = new AllocateOrderShippedRequest();
                Result<AllocateOrderShippedResponse> shipResult = allocateOrderWriteService.shipped(shipRequest);
                if (!shipResult.isSuccess()) {
                    throw new RuntimeException("更新收货单失败");// TODO: 2021/9/22 定义统一的异常
                }
            } else {

            }

        }

        return null;
    }
}
